package com.sea.supply.controller;


import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.product.service.CategoryService;
import com.sea.product.service.ChannelService;
import com.sea.supply.entity.Produce;
import com.sea.supply.entity.QualityProduce;
import com.sea.supply.entity.QualityProduceType;
import com.sea.supply.service.QualityProduceService;
import com.sea.supply.service.QualityProduceTypeService;
import com.sea.supply.service.QualityReportService;
import com.sea.supply.service.SupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/quality/produce/type")
public class QualityProduceTypeController {
    @Autowired
    private ChannelService channelService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private QualityReportService qualityReportService;
    @Autowired
    private QualityProduceTypeService qualityProduceTypeService;

    @RequestMapping("/main")
    public String orderDateMain(Model model) {
        model.addAttribute("category",categoryService.findCategory());
        model.addAttribute("channel", channelService.findByCnd(null));
        model.addAttribute("supplier", supplierService.findByCnd(null));
        return "/sea/supply/quality/produce/type/main";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                         @RequestParam(value = "offset", defaultValue = "0") int start,
                                     QualityProduceType qualityProduceType

    ) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<QualityProduceType> list = qualityProduceTypeService.findByCnd(qualityProduceType);
        PageInfo<QualityProduceType> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 新增
     * @param qualityProduceType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(QualityProduceType qualityProduceType) {
        AjaxReturn result = Ajax.fail().setMsg("新增失败！");
        try{
            if (null != qualityProduceType && qualityProduceTypeService.save(qualityProduceType) > 0) {
                result.setOk(true).setMsg("新增成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxReturn delete(Integer id) {
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try{
            if (null != id && qualityProduceTypeService.delete(id) > 0) {
                result.setOk(true).setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
